package com.lcicl.reggie.controller;
/*
  Date: 2023/10/31
  Author: Luochao
  Description:
*/

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lcicl.reggie.common.BaseContext;
import com.lcicl.reggie.common.R;
import com.lcicl.reggie.entity.AddressBook;
import com.lcicl.reggie.service.AddressBookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    AddressBookService addressBookService;


    /**
     * 展示用户默认地址
     *
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault() {
        Long currentId = BaseContext.getCurrentId();
        System.out.println(currentId);
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, currentId)
                .eq(AddressBook::getIsDefault, 1);

        AddressBook one = addressBookService.getOne(queryWrapper);
        return R.success(one);
    }

    /**
     * 展示所有地址
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list() {
        Long currentId = BaseContext.getCurrentId();

        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, currentId)
                .orderByDesc(AddressBook::getIsDefault);

        List<AddressBook> addressBooks = addressBookService.list(queryWrapper);

        return addressBooks.size()>0 ? R.success(addressBooks): R.error("请新建地址");
    }

    /**
     * 查询单个地址
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> get(@PathVariable(value = "id") Long id) {
        System.out.println(id);
        AddressBook byId = addressBookService.getById(id);
        return R.success(byId);
    }

    /**
     * 修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping()
    public R<String> update(@RequestBody AddressBook addressBook) {
        Long currentId = BaseContext.getCurrentId();
        addressBook.setUserId(currentId);

        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

    @PostMapping()
    public R<String> save(@RequestBody AddressBook addressBook) {
        Long currentId = BaseContext.getCurrentId();
        addressBook.setUserId(currentId);

        addressBookService.save(addressBook);
        return R.success("添加成功");
    }


    @PutMapping("/default")
    public R<String> changeDefault(@RequestBody AddressBook addressBook) {
        Long currentId = BaseContext.getCurrentId();
        addressBook.setUserId(currentId);

        if (addressBook.getId() == null) {
            return R.error("错误");
        }

        boolean flag = addressBookService.changeDefault(addressBook);
        return R.error(flag ? "修改成功" : "修改失败");

    }



}
